
<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div class="filter-container tw-filter">
      <el-button class="filter-item tw-filter-button" size="medium" icon="el-icon-edit" v-has="$store.state.user.btn_common+'add'" @click="handleAdd()">添加</el-button>
      <el-button class="filter-item tw-filter-button" size="medium" icon="el-icon-delete" v-has="$store.state.user.btn_common+'del'" @click="handleDelete()">批量删除</el-button>
    </div>

    <!-- 表格内容 -->
    <el-table
      v-loading="listLoading"
      :data="tableList"
      @selection-change="handleSelectionChange"
      border fit highlight-current-row style="width: 100%;"
    >
      <el-table-column type="selection" width="50" />
      <el-table-column label="备份名" prop="name" align="left" />
      <el-table-column label="备份大小" prop="size" align="left" />
      <el-table-column label="备份时间" prop="make_time" align="left" />
      <el-table-column label="操作" align="center" width="200px" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <a :href="row.upload_url" :download="row.name" target="_blank" v-has="$store.state.user.btn_common+'download'" class="tw-m-r-10">
            <el-button size="mini">下载</el-button>
          </a>
          <el-button size="mini" v-has="$store.state.user.btn_common+'del'" @click="handleDelete(row.name)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="isPage && listTotal > 0" :total="listTotal" :page.sync="queryList.curpage" :limit.sync="queryList.page" @pagination="getList" />

    <!-- 添加/编辑 -->
    <el-dialog width="40%" @close="resetForm()" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form label-position="right" label-width="120px" ref="dataForm" :rules="rules" :model="form">
        <el-form-item label="备份名" prop="backup_name">
          <el-input v-model="form.backup_name" placeholder="请输入备份名" />
          <p class="tw-tip-text">由1到20位数字、字母或下划线组成</p>
        </el-form-item>
        <el-form-item label="分卷大小" prop="file_size">
          <el-input v-model="form.file_size" placeholder="请输入分卷大小" />
          <p class="tw-tip-text">整数 单位：KB【至少为10KB】</p>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogStatus==='create'?createData():updateData()">保存</el-button>
        <el-button @click="dialogFormVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // 基于el分页的二次封装
import waves from '@/directive/waves' // waves directive

export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      // 数据列表
      listLoading: true,
      listTotal: 0,
      isPage: false,
      tableList: [],
      multipleSelection: [],//多选数据
      // 添加/编辑弹窗
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
      	update: '编辑',
      	create: '添加'
      },
      //键字段
      keyName: 'name',
      apiPath: 'project.databasebackup',

      // 搜索数据
      queryList: {
        page: this.$store.state.settings.page, // 每页显示数量
        curpage: 1, // 当前页码
      },

      // 表单数据
      form: {
        backup_name: '',
        file_size: 204800,
      },
      // 重置数据
      formReset: {
        backup_name: '',
        file_size: 204800,
      },
      // 验证
      rules: {
        backup_name: [{ required: true, message: '请输入备份名！', trigger: 'change' }],
        file_size: [{ required: true, message: '请输入分卷大小！', trigger: 'change' }],
      }
    }
  },
  created() {
    //获取数据列表
    this.getList()
  },
  methods: {
    // 搜索
    handleFilter() {
    	this.queryList.curpage = 1
    	this.tableList = []
      this.listTotal = 0
    	this.getList()
    },
    // 列表选择
    handleSelectionChange(val) {
    	this.multipleSelection = val
    },
    // 重置form
    resetForm() {
    	this.form = JSON.parse(JSON.stringify(this.formReset))
    },
    // 获取数据列表
    getList() {
    	this.listLoading = true
    	this.$api.fetchList(this.queryList, this.apiPath).then(response => {
    		this.tableList = response.list
    		this.listTotal = response.list_count
    		this.isPage = response.is_page
        this.listLoading = false
    	})
    },
    // 添加
    handleAdd() {
      var d = new Date();
      this.form.backup_name = d.getFullYear() + '' + d.getMonth() + '' + d.getDate() + '' + d.getHours() + '' + d.getMinutes() + '' + d.getMilliseconds()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 添加-数据提交
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$api.add(this.form, this.apiPath).then(() => {
            // 重新获取列表
            this.queryList.curpage = 1
            this.tableList = []
            this.getList()

            this.dialogFormVisible = false
            this.$message({
              message: '备份成功',
              type: 'success'
            })
          })
        }
      })
    },
    // 删除
    handleDelete(id = '') {
      let current = this
      let id_str = ''
      if (!id) {
        // 多选
        if (!this.multipleSelection.length) {
          this.$message({
            message: '请选择删除的条目！',
            type: 'warning'
          })
          return false
        }
        // 数据处理
        this.multipleSelection.forEach(item => {
          id_str += item[current.keyName] + ','
        })
      } else {
        id_str = id
      }

      // 提交数据
      this.$confirm('确认删除数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.del({ id_str: id_str }, this.apiPath).then(response => {
          // 重新获取列表
          this.getList()
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        })
      })
    }
    
  }
}
</script>
